package com.cn.wanxi.servlet.back.product;

import com.cn.wanxi.model.ProductTypeModel;
import com.cn.wanxi.service.IProductTypeService;
import com.cn.wanxi.service.impl.ProductTypeServiceImpl;
import com.cn.wanxi.util.DataEncoding;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/back/product/type/manage")
public class ProductTypeManageServlet extends HttpServlet {
    IProductTypeService productTypeService = new ProductTypeServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DataEncoding.setTextContentType(resp);
        DataEncoding.setCharacterEncoding(req,resp);
        //产品类型删除
        String ptid = req.getParameter("id");
        int id = 0;
        if (!"".equals(ptid)) {
            try {
                id = Integer.parseInt(ptid);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        int result = productTypeService.deleteProductType(id);
        resp.getWriter().print(result);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        产品类型修改
        DataEncoding.setTextContentType(resp);
        DataEncoding.setCharacterEncoding(req,resp);
        ProductTypeModel productType = new ProductTypeModel();
        String ptid = req.getParameter("id");
        int id = 0;
        if (!"".equals(ptid)) {
            try {
                id = Integer.parseInt(ptid);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        productType.setId(id);
        productType.setName(req.getParameter("name"));
        productType.setUpdate_time(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date().getTime()));
        int result = productTypeService.updateProductType(productType);
        resp.getWriter().print(result);
    }
}
